<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="场次id" prop="gameNum">
        <el-input
          v-model="queryParams.gameNum"
          placeholder="请输入游戏场次id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="游戏类型" prop="gameId">
        <el-select v-model="queryParams.gameId" placeholder="请选择游戏类型" clearable >
          <el-option v-for="gm in gmList"
                     :key="gm.gameId"
                     :label="gm.gameName"
                     :value="gm.gameId"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="店长ID" prop="shopManagerId">
        <el-input
          v-model="queryParams.shopManagerId"
          placeholder="请输入店长ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['metaverse-gameRecord:gameRecord:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['metaverse-gameRecord:gameRecord:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['metaverse-gameRecord:gameRecord:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['metaverse-gameRecord:gameRecord:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="gameRecordList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="游戏场次的序号" align="center" prop="gameNum" />
      <el-table-column label="游戏名字" align="center" prop="gameName" />
      <el-table-column label="游戏标题" align="center" prop="gameTitle" />
      <el-table-column label="奖品1" align="center" prop="prizeOne" />
      <el-table-column label="奖品2" align="center" prop="prizeTwo" />
      <el-table-column label="奖品3" align="center" prop="prizeThree" />
      <el-table-column label="游戏状态" align="center" prop="status" :formatter="formartterStatus"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            @click="sel(scope.row.gameNum)"
            type="text"
            size="mini"
            icon="el-icon-edit"
          >详情</el-button>
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-edit"-->
<!--            @click="handleUpdate(scope.row)"-->
<!--            v-hasPermi="['metaverse-gameRecord:gameRecord:edit']"-->
<!--          >修改</el-button>-->
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-delete"-->
<!--            @click="handleDelete(scope.row)"-->
<!--            v-hasPermi="['metaverse-gameRecord:gameRecord:remove']"-->
<!--          >删除</el-button>-->
          <el-button
            @click="update(scope.row.gameNum)"
            type="primary"
            size="mini"
            :disabled="gameStarted"
          >开始</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />


    <!-- 游戏详情 -->
    <el-drawer
      title="游戏详情"
      :visible.sync="drawer"
      direction="rtl"
      size="350px">
      <div class="text_style">
        <tr>
          <td>创建人：{{form.createBy}}</td>
        </tr>
        <tr>
          <td>创建时间：{{form.createTime}}</td>
        </tr>
        <tr>
          <td>持续时间：{{form.gameDuration}}</td>
        </tr>
        <tr>
          <td>店长ID：{{form.shopManagerId}}</td>
        </tr>
        <tr>
          <td>店铺ID：{{form.shopId}}</td>
        </tr>
        <tr>
          <td>修改人：{{form.updateBy}}</td>
        </tr>
        <tr>
          <td>修改时间：{{form.updateTime}}</td>
        </tr>
        <tr v-if="form.gameId != 4">
          <td>第一名：{{form.nickNameOne}}</td>
        </tr>
        <tr v-if="form.gameId == 1">
          <td>第二名：{{form.nickNameTwo}}</td>
        </tr>
        <tr v-if="form.gameId == 1">
          <td>第三名：{{form.nickNameThree}}</td>
        </tr>
        <tr v-if="form.gameId == 3 || form.gameId == 4">
          <td>最低参与人数：{{form.playerCountMin}}</td>
        </tr>

        <tr>
          <td>总参与人数：{{form.playerCountTotal}}</td>
        </tr>
        <tr v-if="form.gameId == 3 || form.gameId == 4">
          <td>参与金额：{{form.cost}}</td>
        </tr>
        <tr v-if="form.gameId == 4">
          <td>游戏小局数量：{{form.gameCount}}</td>
        </tr>
        <div v-if="form.gameId == 4">
          <tr v-for="(name,i) in form.nickNameS" :key="form.nickNameS">
            <td>第{{i+1}}局：{{name}}</td>
          </tr>
        </div>
      </div>

    </el-drawer>



    <!-- 添加或修改创建房间对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="游戏类型" prop="gameId">
          <el-select v-model="form.gameId" placeholder="请选择游戏类型">
            <el-option v-for="gm in gmList" :key="gm.gameId" :label="gm.gameName" :value="gm.gameId"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="游戏标题" prop="gameTitle">
          <el-input v-model="form.gameTitle" placeholder="请输入游戏标题" />
        </el-form-item>
        <el-form-item label="游戏时长" prop="gameDuration" v-if="form.gameId == 1">
          <el-radio-group v-model="form.gameDuration">
            <el-radio label="30" value="30">30s</el-radio>
            <el-radio label="60" value="60">60s</el-radio>
            <el-radio label="90" value="90">90s</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="游戏时长" prop="gameDuration" v-if="form.gameId != 1">
          <el-radio-group v-model="form.gameDuration">
            <el-radio label="180" value="180">180s</el-radio>
            <el-radio label="240" value="240">240s</el-radio>
            <el-radio label="300" value="300">300s</el-radio>
            <el-radio label="360" value="360">360s</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="奖品1" prop="prizeOne">
          <el-input v-model="form.prizeOne" placeholder="请输入奖品1" />
        </el-form-item>
        <el-form-item label="奖品2" prop="prizeTwo" v-if="form.gameId == 1">
          <el-input v-model="form.prizeTwo" placeholder="请输入奖品2" />
        </el-form-item>
        <el-form-item label="奖品3" prop="prizeThree" v-if="form.gameId == 1">
          <el-input v-model="form.prizeThree" placeholder="请输入奖品3" />
        </el-form-item>
        <el-form-item label="游戏自动开始时间" prop="gameStartTime">
          <el-radio-group v-model="form.gameStartTime">
            <el-radio label="300" value="300">300s</el-radio>
            <el-radio label="360" value="360">360s</el-radio>
            <el-radio label="420" value="420">420s</el-radio>
            <el-radio label="480" value="480">480s</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="最低参与人数" prop="playerCountMin" v-if="form.gameId == 3 || form.gameId == 4">
          <el-input v-model="form.playerCountMin" placeholder="请输入最低参与人数" />
        </el-form-item>
        <el-form-item label="参与金额" prop="cost" v-if="form.gameId == 3 || form.gameId == 4">
          <el-input v-model="form.cost" placeholder="请输入参与金额" />
        </el-form-item>
        <el-form-item label="游戏小局数量" prop="gameCount" v-if="form.gameId == 4">
          <el-input v-model="form.gameCount" placeholder="请输入游戏小局数量" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listGameRecord, getGameRecord, delGameRecord, addGameRecord, updateGameRecord, updateStatus } from "@/api/metaverse-gameRecord/gameRecord";
import {listGame} from "../../../api/metaverse-game/game";

export default {
  name: "GameRecord",
  data() {
    return {
      gmList:[],
      drawer: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 创建房间表格数据
      gameRecordList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        gameNum: null,
        gameId: null,
        gameStartTime: null,
        gameDuration: null,
        gameTitle: null,
        shopManagerId: null,
        shopId: null,
        prizeOne: null,
        prizeTwo: null,
        prizeThree: null,
        status: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        playerCountMin: null,
        playerCountTotal: null,
        cost: null,
        gameCount: null,
        nickNameS: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    listGame().then(resp => {
      this.gmList = resp.rows
    })
    this.getList();

  },
  methods: {
    /** 查询创建房间列表 */
    getList() {
      this.loading = true;
      listGameRecord(this.queryParams).then(response => {
        console.log("数据：",response.rows)
        this.gameRecordList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        gameNum: null,
        gameId: null,
        gameStartTime: null,
        gameDuration: null,
        gameTitle: null,
        shopManagerId: null,
        shopId: null,
        prizeOne: null,
        prizeTwo: null,
        prizeThree: null,
        status: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        playerCountMin: null,
        playerCountTotal: null,
        cost: null,
        gameCount: null,
        nickNameS: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.gameNum)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加创建房间";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const gameNum = row.gameNum || this.ids
      getGameRecord(gameNum).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改创建房间";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.gameNum != null) {
            updateGameRecord(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addGameRecord(this.form).then(response => {
              console.log(response)
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });

          }

        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const gameNums = row.gameNum || this.ids;
      this.$modal.confirm('是否确认删除创建房间编号为"' + gameNums + '"的数据项？').then(function() {
        return delGameRecord(gameNums);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('metaverse-gameRecord/gameRecord/export', {
        ...this.queryParams
      }, `gameRecord_${new Date().getTime()}.xlsx`)
    },
    formartterStatus(row){
      if (row.status == 0){
        this.gameStarted = true
        return '游戏开始'
      }else if (row.status == 1){
        this.gameStarted = false
        return '创建房间未开启游戏'
      }else if (row.status==2){
        this.gameStarted = true
        return '游戏结束'
      }
    },

    /**
     * 游戏详情
     * @param gameNum
     */
    sel(gameNum){
      this.drawer = true;
      const 游戏详情 = this.title = '游戏详情';
      getGameRecord(gameNum).then(resp => {
        console.log("详细信息接口返回：", resp)
        this.form = resp.data;
      })
    },

    update(gameNum){
      // this.$confirm('确定要开始游戏吗?', '提示', {
      //   confirmButtonText: '确定',
      //   cancelButtonText: '取消',
      //   type: 'warning'
      // }).then(() => {
        updateStatus(gameNum).then(resp=>{
          console.log(resp.data)
          if (resp.code == 200){
            this.$message.success('开始游戏成功');
            this.getList();
          }else{
            this.$message.success('已取消开始游戏');
          }
        })
      // }).catch(() => {
      //   this.$message.info('已取消开始游戏');
      // })
    },
  }
};
</script>
<style>
.text_style {
  color: gray;
  padding-left: 20px;
  font-size: 16px;
  line-height: 40px;

}
</style>
